﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class CategoryDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get all categories in DB
        /// </summary>
        /// <returns></returns>
        public List<Category> GetListCategories()
        {
            try
            {
                var resultQuery = from category in _entities.Categories
                                  where category.IsDeleted == false
                                  select category;
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get a category by CategoryId
        /// </summary>
        /// <param name="categoryId">CategoryId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public Category GetCategoryById(int categoryId)
        {
            try
            {
                var resultQuery = (from category in _entities.Categories
                                   where category.ID == categoryId
                                         && category.IsDeleted == false
                                   select category).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get a category by name
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public Category GetCategoryByName(string categoryName)
        {
            try
            {
                var resultSelect = (from category in _entities.Categories
                                    where category.Name == categoryName
                                          && category.IsDeleted == false
                                    select category).FirstOrDefault();
                return resultSelect;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Create a category
        /// </summary>
        /// <param name="category">Category object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool CreateCategory(Category category)
        {
            try
            {
                _entities.Categories.Add(category);
                var resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Update a category
        /// </summary>
        /// <param name="areaId">Category object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool UpdateCategory(Category category)
        {
            try
            {
                _entities.Entry(category).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Delete a categoy
        /// </summary>
        /// <param name="categoryId">CategoryId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool DeleteCategory(int categoryId)
        {
            try
            {
                Category categoryEnt = new Category();
                categoryEnt = (from category in _entities.Categories
                               where category.ID == categoryId
                               select category).FirstOrDefault();
                categoryEnt.IsDeleted = true;
                _entities.Entry(categoryEnt).State = EntityState.Modified;
                var resultDelete = _entities.SaveChanges();
                if (resultDelete > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Check is existed a category when update a category
        /// </summary>
        /// <param name="categoryId">Id</param>
        /// <param name="categoryName">Name</param>
        /// <returns>True: Existed -- False: Not existed</returns>
        public bool IsExistedCategoryUpdate(int categoryId, string categoryName)
        {
            try
            {
                var selectResult = (from category in _entities.Categories
                                    where category.ID != categoryId
                                          && category.Name == categoryName
                                    select category).FirstOrDefault();
                if (selectResult != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}